import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { AlertController } from '@ionic/angular';
import { login } from '../api/request';
import { insertLog } from '../api/api-all';

@Component({
  selector: 'app-page-login',
  templateUrl: './page-login.component.html',
  styleUrls: ['./page-login.component.scss']
})
export class PageLoginComponent {

  constructor(private router: Router, private alertController: AlertController) { }

  username: string = ""

  password: string = ""

  submitLogin() {
    login(this.username, this.password).then(res => {
      console.log(res)
      // 登录成功
      insertLog({userId: res.id, remark: "登录上线"})
      this.router.navigate(['/starnight-student/dashboard'])
    }).catch(async (e) => {
      const alert = await this.alertController.create({
        message: '登录失败，账号或密码错误',
        buttons: ['OK'],
      })
      await alert.present()
      this.username = ""
      this.password = ""
    })
  }

  goRigPage() {
    this.router.navigate(['/starnight-student/reg'])
  }

}
